package com.hgk.wanwan.main.fragment.pushfragment.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.handmark.pulltorefresh.library.internal.BallBaseAdapter;
import com.common.nostra13.universalimageloader.core.DisplayImageOptions;
import com.common.nostra13.universalimageloader.core.ImageLoader;
import com.hgk.common.common.ui.model.PhotosBean;
import com.hgk.common.common.util.LoadImageDisplayOption;

import width.hgk.wanwan.R;

/**
 * Created by lenovo on 2016/10/11.
 */
public class PushGridAdapter extends BallBaseAdapter<PhotosBean> {
    private Context mContext;
    private LayoutInflater inflater;
    private DisplayImageOptions mdisplayoutImageOptions;
    private DelePicIconListener mDelepic;
    public PushGridAdapter(Context context,DelePicIconListener listener) {
        super(context);
        this.mContext = context;
        this.mDelepic = listener;
        inflater = LayoutInflater.from(mContext);
        mdisplayoutImageOptions = LoadImageDisplayOption.getDefaultIcon(R.drawable.add_icon_option);

    }
    public interface DelePicIconListener{
        void OnCliclike(PhotosBean data,ViewHolder vh);
    }
    public class ViewHolder{
        private ImageView mIvTitlepic,mIvDelePic;
        private TextView mPicName;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder vh = null;
        if (convertView==null){
            vh = new ViewHolder();
            convertView = inflater.inflate(R.layout.push_gridview_item,null);
            vh.mIvDelePic = (ImageView) convertView.findViewById(R.id.iv_push_grid_item_dele);
            vh.mIvTitlepic = (ImageView) convertView.findViewById(R.id.iv_push_pic_title);
            vh.mPicName = (TextView) convertView.findViewById(R.id.tv_push_gridview_item_pic_name);
            convertView.setTag(vh);
        }else{
            vh= (ViewHolder) convertView.getTag();
        }
        final PhotosBean bean = getItem(position);
        if (bean!=null&&!TextUtils.isEmpty(bean.getPic_path())){
            if ("-1".equals(bean.getPic_path())){
                vh.mIvTitlepic.setImageResource(R.drawable.add_icon_option);
                vh.mPicName.setVisibility(View.GONE);
                vh.mIvDelePic.setVisibility(View.GONE);
            }else {
                String path = bean.getPic_path();
                if (path.startsWith("/")){
                    path = "file://"+path;
                }
                if (!TextUtils.isEmpty(bean.getPicName())){
                    vh.mPicName.setVisibility(View.VISIBLE);
                }else{
                    vh.mPicName.setVisibility(View.GONE);
                }
                vh.mIvDelePic.setVisibility(View.VISIBLE);
                ImageLoader.getInstance().displayImage(path,vh.mIvTitlepic,mdisplayoutImageOptions);
            }
        }
        return convertView;
    }
}
